package com.aaa.ssm.service.impl;

import com.aaa.ssm.entity.Emp;
import com.aaa.ssm.entity.Permission;
import com.aaa.ssm.exception.LoginException;
import com.aaa.ssm.mapper.EmpMapper;
import com.aaa.ssm.mapper.PermissionMapper;
import com.aaa.ssm.service.EmpService;
import com.aaa.ssm.sysconst.EmpState;
import com.aaa.ssm.sysconst.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author AUSU
 * @version 1.0
 * 创建时间：2025/3/20 15:17
 */
@Service
public class  EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> getPermission(Integer rid) throws Exception {
        if (rid == Role.SUPER_USER){
            return permissionMapper.getAllPermissions();
        }
        return permissionMapper.getPermissionsByRid(rid);
    }

    @Override
    public Emp login(String username, String password) throws LoginException{

        //1.验证账号密码是否正确,查询员工eid
        Long eid = empMapper.selectEid(username,password);
        if (eid == null){
            throw new LoginException("账号密码错误");
        }
        //2.根据eid 查询员工信息
        Emp emp = empMapper.getEmpByEid(eid);
        Integer state = emp.getState();
        if (state.equals(EmpState.EMP_FREEZE)) {
            throw new LoginException("账号被冻结");
        } else if (state.equals(EmpState.EMP_RESIGN)){
            throw new LoginException("员工离职");
        }
        //3.黑名单 白名单 员工是否冻结

        return emp;
    }
}
